// File : common.js

function toggleVisibility(element) {
    if(element.style.display != 'none') element.style.display = 'none';
    else element.style.display = '';
}

function toggleVisibilityId(id) {
    toggleVisibility(document.getElementById(id));
}

function setVisibileAndHideOther(show_id, hide_ids) {
    var doc = document.getElementById(show_id);
    doc.style.display = '';

    for(key in hide_ids) {
        doc = document.getElementById(hide_ids[key]);
        doc.style.display = 'none';
    }
}

function toggleArrowBtn(element) {
    var expand  = /expand/;
    if(expand.test(element.src)) element.src = element.src.replace(expand,"collapse");
    else element.src = element.src.replace(/collapse/,"expand");
}

function loadElement(url, destination, par){
    window.event.preventDefault();

    var ajax = new AjaxWrap();

    // Set Loading Image
    var style = "style=\"height:" + destination.offsetHeight + "px; width:" + destination.offsetWidth + "px;\"";
    destination.innerHTML = "<div " + style + " ><img style=\"margin-left: \" src=\"" + URL_BASE + "public/image/loading.gif\" alt=\"login\" /></div>";

    // Set call back function
    ajax.onLoadSuccess = function(ajax){
        destination.innerHTML = ajax.xhr.responseText;
    }

    // Do request
    ajax.openAndSend("POST", url, true, "ajax=true&" + par);

    return false;
}

function $(par){
    var className;
    var result;

    if (par.charAt(0) == ".") {
        className = par.substr(1, par.length - 1);
        result = getElementByClass(className);
    } else if (par.charAt(0) == "#") {
        idName= par.substr(1, par.length - 1);
        result = document.getElementById(idName);
    } else {
        result = document.getElementsByTagName(par);
    }

    return result;
}

function getElementByClass(className, node) {
    var array = new Array();
    
    // Check node
    if (node == null) {
        node = document;
    }

    //Create Array of All HTML Tags
    var tags=node.getElementsByTagName("*");

    //Loop through all tags using a for loop
    for (i=0; i<tags.length; i++) {
        //Get all tags with the specified class name.
        if (tags[i].className == className) {
            array.push(tags[i]);
        }
    }

    return array;
}